//
//  CLEmoiconToolBar.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/19.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit

//定义toolbar底部按钮的三种枚举值
enum CLEmoiconToolBarButtonType:Int {
    case Default = 1000
    case Emoji = 1001
    case Lxh = 1002
}

//表情键盘的底部toolbar
class CLEmoiconToolBar: UIStackView {

    //记录上一次按钮点击的状态的按钮属性
    var lastSelectedButton:UIButton?
    //定义选中toolbar中按钮的闭包
    var selectedButtonClosure:((type:CLEmoiconToolBarButtonType)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        
        //设置strackView的布局方式
        axis = .Horizontal
        //填充方式
        distribution = .FillEqually
        //添加控件
        addChildButton("默认", imageName: "compose_emotion_table_left", type: .Default)
        addChildButton("emoji", imageName: "compose_emotion_table_mid", type:.Emoji)
        addChildButton("浪小花", imageName: "compose_emotion_table_right", type: .Lxh)
    }
    //创建toolbar子控件的函数
    private func addChildButton(title:String ,imageName :String,type:CLEmoiconToolBarButtonType) {
        let button = UIButton()
        //用枚举的原始值 设置为tag值
        button.tag = type.rawValue
        //添加点击事件
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        //设置文字及颜色
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: imageName + "_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: imageName + "_selected"), forState: .Selected)
        //取消高亮状态
        button.adjustsImageWhenHighlighted = false
        //添加按钮
        addArrangedSubview(button)
        //默认选择第一个
        if type == .Default {
            lastSelectedButton?.selected = false
            button.selected = true
            lastSelectedButton = button
        }
        
    }
    //通过section选中某一个按钮
    func selectButtonWithSection(section : Int) {
        //通过section获取到对应的button 让其选中
        let button = viewWithTag(section + 1000)! as! UIButton
//        buttonAction(button)
        changeButtonStatu(button)
    }
    //改变按钮状态,把当前选中的button取消选中 吧传入的button设置选中
    private func changeButtonStatu(button:UIButton) {
        if button == lastSelectedButton {
            return
        }
        //取消选中之前的
        lastSelectedButton?.selected = false
        //选中现在点击的
        button.selected = true
        //再次记录现在选的按钮
        lastSelectedButton = button
        
    }
    //按钮的点击事件 取消上一个按钮的点击选中状态
    @objc private func buttonAction(button:UIButton){
        
        if button == lastSelectedButton {
            return
        }
        changeButtonStatu(button)
        //根据所选按钮的tag  来判断点击的按钮  传递闭包
        let type = CLEmoiconToolBarButtonType(rawValue: button.tag)!
        
        selectedButtonClosure?(type:type)
    }

}
